#include "StdAfx.h"
#include "PhysicsEntity.h"

//PhysicsEntity::PhysicsEntity(
//	OgreBulletDynamics::DynamicsWorld* world,
//	std::deque<OgreBulletDynamics::RigidBody *> rigidBodies,
//	std::deque<OgreBulletCollisions::CollisionShape *> collisionShape,
//	EntityControl* entityControl,
//	Ogre::SceneNode* node, float weight, float box)
//	:OgreBulletDynamics::RigidBody(node->getName()+"RigidBody", world)
//{
//	// Get the model
//	Ogre::Entity* model = entityControl->getModelActor();
//
//	// Bounding box
//	Ogre::AxisAlignedBox boundingBox = model->getBoundingBox();
//	Ogre::Vector3 boundingBoxSize = boundingBox.getSize();
//	boundingBoxSize = boundingBoxSize/2.0;
//	boundingBoxSize = boundingBoxSize*box;
//
//	OgreBulletCollisions::BoxCollisionShape* shape =
//		new OgreBulletCollisions::BoxCollisionShape(boundingBoxSize);
//
//	setShape(node, shape, 0.6f, 0.1f,  weight, entityControl, Ogre::Vector3(0,5,0), Ogre::Quaternion::IDENTITY);
//	rigidBodies.push_back(this);
//	collisionShape.push_back(mShape);
//}
//
//PhysicsEntity::PhysicsEntity(
//	std::deque<OgreBulletDynamics::RigidBody *> rigidBodies,
//	std::deque<OgreBulletCollisions::CollisionShape *> collisionShape,
//	const OgreBulletDynamics::RigidBody& body,
//	OgreBulletCollisions::CollisionShape* shape,
//	EntityControl* entityControl,
//	Ogre::SceneNode* node, float weight)
//	:OgreBulletDynamics::RigidBody(body)
//{
//	setShape(node, shape, 0.6f, 0.1f,  weight, entityControl, Ogre::Vector3(0,5,0), Ogre::Quaternion::IDENTITY);
//
//	rigidBodies.push_back(this);
//	collisionShape.push_back(mShape);
//}


// PhysX part
PhysicsEntity::PhysicsEntity(Ogre::SceneNode* ogreNode,NxActorDesc actorD, NxScene* physXScene, bool isKinematic, bool isStatic, int collisionGroup)
{
		this->ogreNode = ogreNode;
		/*boxDesc.dimensions.set(ogreNode->getScale().x*2.0f,
				ogreNode->getScale().y*2.0f,
				ogreNode->getScale().z*2.0f);*/

		/*for(std::vector<NxShapeDesc*>::iterator i =shapeDesc.begin(); i!= shapeDesc.end(); i++)
		{
			actorDesc.shapes.pushBack((*i));

		}*/

		actorDesc = actorD;

		/*Ogre::Vector3 original = Ogre::Vector3(0,1,0);
		Ogre::Vector3 axis = Ogre::Vector3(0,-1,0);*/

		//Ogre::Quaternion q = original.getRotationTo(axis);
		/*Ogre::Quaternion q;
		q.
		Ogre::Matrix3  turnMatrix;
		q.ToRotationMatrix( turnMatrix);

		actorDesc.globalPose.M.setColumnMajorStride4((NxF32*)&turnMatrix);*/

		if (isStatic) 
			actorDesc.body = 0;
		else
			actorDesc.body = &bodyDesc;
		actorDesc.density = 10;
		actorDesc.globalPose.t 	= NxVec3(ogreNode->getPosition().x,
					ogreNode->getPosition().y,
					ogreNode->getPosition().z);	
		
		bool valid = actorDesc.isValid();
		actor = physXScene->createActor(actorDesc);
		actor->setGlobalOrientationQuat(NxQuat(NxVec3((ogreNode->getOrientation().x),ogreNode->getOrientation().y,ogreNode->getOrientation().z),ogreNode->getOrientation().w));
		if (isKinematic) actor->raiseBodyFlag(NX_BF_KINEMATIC);	//KINEMATIKUS ACTOR

		// Setting CollisionGroup
		actor->setGroup(collisionGroup);
		//Get all shapes of the actor
		NxShape*const* shapes = actor->getShapes();
		int nShapes = actor->getNbShapes();
		for (int i = 0; i <  nShapes; i++)
		{
			// Set the given group for all shapes
			shapes[i]->setGroup(collisionGroup);
		}
}

NxActor* PhysicsEntity::getNxActor()
{
	return this->actor;
}
Ogre::SceneNode* PhysicsEntity::getOgreNode()
{
	return this->ogreNode;
}
PhysicsEntity::~PhysicsEntity(void)
{
}

//void PhysicsEntity::setShape(Ogre::SceneNode *node,
//	OgreBulletCollisions::CollisionShape *shape,
//	const float      bodyRestitution,
//	const float      bodyFriction,
//	const float      bodyMass,
//	EntityControl*   entityControl,
//	const Ogre::Vector3 &pos,
//	const Ogre::Quaternion &quat)
//{
//
//	mState = new OgreBulletCollisions::ObjectState(this);
//
//	mRootNode = node;
//	//mShapeNode = mRootNode->createChildSceneNode(mName + "Node");
//	//mShapeNode->attachObject(this);
//
//	/*node->setPosition (pos);
//	node->setOrientation (quat);*/
//
//	mShape = shape;
//
//	showDebugShape(mWorld->getShowDebugShapes());
//
//	btVector3 localInertiaTensor = btVector3(0,0,0);
//	if (bodyMass > 0.0)
//		mShape->getBulletShape ()->calculateLocalInertia(bodyMass, localInertiaTensor);
//
//	btRigidBody *body = new btRigidBody(bodyMass, mState, mShape->getBulletShape (), localInertiaTensor);
//	body->setRestitution(bodyRestitution);
//	body->setFriction(bodyFriction);
//
//	mObject = body;
//
//	getDynamicsWorld()->addRigidBody(this, mCollisionGroup, mCollisionMask);
//
//
//	body->getCollisionShape()->setUserPointer((void*) entityControl);
//	body->setCollisionFlags(body->getCollisionFlags() |
//		btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
//}